package com.newsystem.Config;

import io.minio.BucketExistsArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.errors.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

@Configuration
public class MinioClientConfig {

    @Value("${minio.endpoint}")
    private String endpoint;
    @Value("${minio.accessKey}")
    private String accessKey;
    @Value("${minio.secretKey}")
    private String secretKey;
    @Value("${minio.bucket}")
    private String bucketName;
    @Bean
    public MinioClient minioClient() {
        MinioClient minioClient =  MinioClient.builder()
                .endpoint(endpoint).credentials(accessKey,secretKey).build();
        try {
            Boolean bucketSxist = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
            if(!bucketSxist){
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return minioClient;
    }
}
